const config = require('_/config');
const multer = require('koa-multer');
const common = require('./common');

const storage = multer.diskStorage({
  destination: (req, _, cb) => {
    const url = req.url;
    switch (url) {
      case '/place/import': cb(null, config.rootPath + '/public/uploads/place');break;
      case '/files/uploads/place': cb(null, config.rootPath + '/public/uploads/place');break;
      case '/files/uploads/wx': cb(null, config.rootPath + '/public/uploads/wx');break;
      case '/files/uploads/car': cb(null, config.rootPath + '/public/uploads/car');break;
      default: cb(null, config.rootPath + '/public/uploads/others');
    }
  },
  filename: function (_, file, cb) {
    const fileNameFormat = (file.originalname).split('.');
		const length = fileNameFormat.length;
    cb(null, `${fileNameFormat.slice(0, -1).join()}-${common.YD_dateFormat(new Date(), 'empty')}.${fileNameFormat[length - 1]}`);
  }
});

const upload = multer({
  storage: storage,
  limits: {
    fields: 10,
    fileSize: 1024 * 1024 * 2,
    files: 1
  }
});

module.exports = upload;
